package de.breitbeil.jbooking.ui.components;

import java.awt.Graphics;
import java.awt.Polygon;
import java.util.Calendar;
import java.util.Date;

import org.jdesktop.swingx.plaf.basic.BasicMonthViewUI;

import de.breitbeil.jbooking.domain.Booking;

public class BookingCalendarUi extends BasicMonthViewUI {

    @Override
    protected void paintDayBackground(Graphics g, int x, int y, int width, int height, Calendar cal) {
        Date date = cal.getTime(); //InMillis();

        BookingCalendarComponent monthView = (BookingCalendarComponent) this.monthView;
        Booking bookingStart = monthView.getBookingForStartDate(date);
        Booking bookingInner = monthView.getBookingForInnerDate(date);
        Booking bookingEnd = monthView.getBookingForEndDate(date);
        
        int borderY = 2;
        int borderX = 3; // Space between two bookings
        int upperCornerY = y + borderY;
        int lowerCornerY = y + height - borderY;
        
        
        if (bookingStart != null) {
            Polygon p = new Polygon();
            p.addPoint(x + width/2, lowerCornerY);
            p.addPoint(x + width, upperCornerY);
            p.addPoint(x + width, lowerCornerY);
            
            g.setColor(monthView.getColorForBooking(bookingStart));
            g.fillPolygon(p);
        }        
        if (bookingEnd != null) {
            Polygon p = new Polygon();
            p.addPoint(x, upperCornerY);
            p.addPoint(x + width - borderX, upperCornerY);
            p.addPoint(x + width/2 - borderX, lowerCornerY);
            p.addPoint(x, lowerCornerY);
            
            g.setColor(monthView.getColorForBooking(bookingEnd));
            g.fillPolygon(p);
        }
        if (bookingInner != null) {
            g.setColor(monthView.getColorForBooking(bookingInner));
            g.fillRect(x, upperCornerY, width, height - 2*borderY);
        }
        
        /*if (monthView.isSelected(date)) {
            g.setColor(monthView.getSelectedBackground());
            g.fillRect(x, y, width, height);
        }*/

        // If the date is today make sure we draw it's background over the selected
        // background.
        /*if (isToday(date)) {
            g.setColor(monthView.getTodayBackground());
            g.drawRect(x, y, width - 1, height - 1);
        }*/
    }
    
    protected void paintMonthStringForeground(Graphics g, String monthName, int monthX, int monthY, 
            String yearName, int yearX, int yearY, Calendar cal) {
        
        // As we don't print the year, center the month at bit
        monthX = monthX + ((yearX - monthX) / 4);
        
        g.drawString(monthName, monthX, monthY);
        //g.drawString(yearName, yearX, yearY);
    }
    
}
